package com.zqxq.lottery.cms.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.auth.entity.module.Code;
import com.zqxq.common.bo.BaseBO;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.cms.feign.LotteryServerFeignClient;
import com.zqxq.lottery.common.utils.ExcelUtils;
import com.zqxq.lottery.entity.bo.sys.*;
import com.zqxq.lottery.entity.po.jczq.JczqMatchLivePO;
import com.zqxq.lottery.entity.po.jczq.JczqMatchPO;
import com.zqxq.lottery.entity.vo.sys.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
@Api(value = "后台管理",tags = "后台管理")
@RestController
@Slf4j
@RequestMapping(value = "/sys", produces = "application/json")
public class SysController {

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @ApiOperation(value = "竞猜种类-分页查询", notes="竞猜种类-分页查询")
    @PostMapping(value="/lotterykind/page")
    public Result<Page<List<LotteryKindVO>>> queryLotteryKindPage(LotteryKindBO search){
        return lotteryServerFeignClient.queryLotteryKindPage(search);
    }

    @ApiOperation(value = "竞猜种类-下拉列表", notes="竞猜种类-下拉列表")
    @PostMapping(value="/lotterykind/list")
    public Result<List<LotteryKindVO>> queryLotteryKindList(LotteryKindBO search){
        return lotteryServerFeignClient.queryLotteryKindList(search);
    }

    @ApiOperation(value = "竞猜种类-新增、修改", notes="竞猜种类-新增、修改")
    @PostMapping(value="/lotterykind/add")
    public Result addLotteryKind(LotteryKindVO vo){
        return lotteryServerFeignClient.addLotteryKind(vo);
    }

    @ApiOperation(value = "竞猜种类-详情", notes="竞猜种类-详情")
    @PostMapping(value="/lotterykind/get")
    public Result getLotteryKind(Long id){
        return lotteryServerFeignClient.getLotteryKind(id);
    }

    @ApiOperation(value = "竞猜种类-状态修改", notes="竞猜种类-状态修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "竞猜种类ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "状态 0-隐藏；1-显示", required = true, dataType = "Integer")
    })
    @PostMapping(value="/lotterykind/updateStatus")
    public Result updateLotteryKindStatus(Long id, int status){
        return lotteryServerFeignClient.updateLotteryKindStatus(id,status);
    }

    @ApiOperation(value = "竞猜种类-排序修改", notes="竞猜种类-排序修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "竞猜种类ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "sort", value = "排序号", required = true, dataType = "Integer")
    })
    @PostMapping(value="/lotterykind/updateSort")
    public Result updateLotteryKindSort(Long id, int sort){
        return lotteryServerFeignClient.updateLotteryKindSort(id,sort);
    }

    @ApiOperation(value = "历史开奖-分页查询", notes="历史开奖-分页查询")
    @PostMapping(value="/lotteryhnum/page")
    public Result<Page<List<LotteryHnumVO>>> queryLotteryKindPage(LotteryHnumBO search){
        return lotteryServerFeignClient.queryLotteryHnumPage(search);
    }

    @ApiOperation(value = "开奖-关闭上期生成下期", notes="开奖-关闭上期生成下期")
    @PostMapping(value="/lotteryhnum/newNext")
    public Result<LotteryHnumVO> newNextLotteryHnum(Long lotteryId){
        return lotteryServerFeignClient.newNextLotteryHnum(lotteryId);
    }

    @ApiOperation(value = "开奖结果-数据同步", notes="开奖结果-数据同步")
    @PostMapping(value="/lotteryhnum/syncFromJisu")
    public Result syncLotteryHnumFromJisu(Long lotteryId,String period){
        return lotteryServerFeignClient.syncLotteryHnumFromJisu(lotteryId,period);
    }


    /***********************************************banner*******************************************************/
    @ApiOperation("分页获取banner列表")
    @PostMapping("/banner/queryPageList")
    public Result<Page<SysBannerVO>> queryBannerPageList(BaseBO baseBO){
        return lotteryServerFeignClient.queryBannerPageList(baseBO);
    }

    @ApiOperation("保存banner信息")
    @PostMapping("/banner/save")
    public Result saveBanner(SysBannerVO vo){
        return lotteryServerFeignClient.saveBanner(vo);
    }

    @ApiOperation("保存banner点击数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "bannerId", required = true, dataType = "String")
    })
    @PostMapping("/banner/saveClickNum")
    public Result saveBannerClickNum(Long id){
        return lotteryServerFeignClient.saveBannerClickNum(id);
    }

    @ApiOperation("关闭banner(删除banner)")
    @PostMapping("/banner/close")
    public Result closeBanner(Long id){
        log.info("删除banner={}",id);
        return lotteryServerFeignClient.closeBanner(id);
    }

    /***********************************************发现*******************************************************/
    @ApiOperation("分页获取发现列表")
    @PostMapping("/discovery/queryPageList")
    public Result<Page<SysDiscoveryVO>> queryDiscoveryPageList(SysDiscoveryCMSBO cmsbo){
        return lotteryServerFeignClient.queryDiscoveryPageList(cmsbo);
    }

    @ApiOperation("保存发现信息")
    @PostMapping("/discovery/save")
    public Result saveDiscovery(SysDiscoveryVO vo){
        return lotteryServerFeignClient.saveDiscovery(vo);
    }

    @ApiOperation("删除发现信息(关闭)")
    @PostMapping("/discovery/close")
    public Result closeDiscovery(@ApiParam(value = "id",required = true) Long id){
        log.info("删除发现信息 id={}",id);
        if (id == null){
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        return lotteryServerFeignClient.closeDiscovery(id);
    }

    /***********************************************消息*******************************************************/

    @ApiOperation("消息公告获取分页列表")
    @PostMapping("/queryMsgPageList")
    public Result<Page<SysMsgVO>> queryMsgPageList(SysMsgCMSBO baseBO){
        return lotteryServerFeignClient.queryMsgPageList(baseBO);
    }

    @ApiOperation("消息公告保存")
    @PostMapping("/saveMsg")
    public Result saveMsg(SysMsgVO vo){
        return lotteryServerFeignClient.saveMsg(vo);
    }

    @ApiOperation("消息公告修改")
    @PostMapping("/updateMsg")
    public Result updateMsg(){
        return lotteryServerFeignClient.updateMsg();
    }

    @ApiOperation("消息公告删除")
    @PostMapping("/deleteMsg")
    public Result deleteMsg(Long id){
        return lotteryServerFeignClient.deleteMsg(id);
    }

    /***********************************************咨询*******************************************************/
    @ApiOperation("资讯获取分页列表")
    @PostMapping("/queryNewsPageList")
    public Result<Page<SysNewsVO>> queryNewsPageList(SysNewsCMSBO baseBO){
        return lotteryServerFeignClient.queryNewsPageList(baseBO);
    }
    @ApiOperation("获取咨询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long")
    })
    @PostMapping("/getNews")
    public Result<SysNewsVO> getNews(Long id){
        if(id == null){
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        return lotteryServerFeignClient.getNews(id);
    }
    @ApiOperation("资讯保存")
    @PostMapping("/saveNews")
    public Result saveNews(SysNewsVO vo){
        return lotteryServerFeignClient.saveNews(vo);
    }

    @ApiOperation("资讯关闭")
    @PostMapping("/closeNews")
    public Result closeNews(@ApiParam(value = "id",required = true) Long id){
        if (id == null){
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        log.info("资讯关闭 id={}",id);
        return lotteryServerFeignClient.closeNews(id);
    }

    /***********************************************数据分析统计*******************************************************/
    @ApiOperation("商户数据统计")
    @PostMapping("/merchantDataCount")
    public Result getMerchantUserOrder(@ApiParam(value = "页码",required = true) @RequestParam(defaultValue = "1") String pageNo,
                                       @ApiParam(value = "每页大小",required = true) @RequestParam(defaultValue = "10") String pageSize,
                                       @ApiParam(value = "开始时间",required = false) String startTime,
                                       @ApiParam(value = "结束时间",required = false) String endTime,
                                       @ApiParam(value = "彩种",required = false) String lotteryId,
                                       @ApiParam(value = "出票状态",required = false) String status){
        if (StrUtils.isEmpty(status)) {
            status = "0";
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("pageNo",pageNo);
        paramMap.put("pageSize",pageSize);
        paramMap.put("startTime",startTime);
        paramMap.put("endTime",endTime);
        paramMap.put("lotteryId",lotteryId);
        paramMap.put("status",status);
        return lotteryServerFeignClient.getMerchantUserOrder(paramMap);
    }
    @ApiOperation("确认出票和取消订单")
    @PostMapping("/updateUserOrderStatus")
    public Result updateUserOrderStatus(@ApiParam(value = "订单编号",required = false) String orderId,
                                        @ApiParam(value = "操作状态（0为确认出票，1为取消订单）",required = false) String status){
        if (StrUtils.isEmpty(orderId)) {
            return new Result(Code.FAIL, "订单编号不能为空！");
        }
        if (StrUtils.isEmpty(status)) {
            return new Result(Code.FAIL, "操作状态不能为空！");
        }
        return lotteryServerFeignClient.merchchantConfirmTicketOrCancelOrder(Long.valueOf(orderId),Integer.valueOf(status));
    }

    @ApiOperation("渠道统计数据导出")
    @RequestMapping("/exportChannelData")
    public Result exportChannelData(HttpServletResponse response,
                                    @ApiParam(value = "页码",required = true) @RequestParam(defaultValue = "1") String pageNo,
                                    @ApiParam(value = "每页大小",required = true) @RequestParam(defaultValue = "10") String pageSize,
                                    @ApiParam(value = "开始时间",required = false) String startTime,
                                    @ApiParam(value = "结束时间",required = false) String endTime,
                                    @ApiParam(value = "平台",required = false) String source,
                                    @ApiParam(value = "渠道",required = false) String channel){

        log.info("渠道统计 pageNo={},pageSize={},startTime={},endTime={},source={},channel={}",pageNo,pageSize,startTime,endTime,source,channel);
        if (StrUtils.isEmpty(startTime) || StrUtils.isEmpty(endTime)){
            //初始查询化时间
            endTime = DateUtils.getCurrentDateStr();
            startTime = DateUtils.dateToStr(DateUtils.addDays(new Date(),-30),"yyyy-MM-dd");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("pageNo",pageNo);
        paramMap.put("pageSize",pageSize);
        paramMap.put("startTime",startTime);
        paramMap.put("endTime",endTime);
        paramMap.put("source",source);
        paramMap.put("channel",channel);
        Result result = lotteryServerFeignClient.getChannelCountList(paramMap);
        if (result.success()){
            if (result.getData() instanceof ArrayList<?>){
                List<ChannelDataExportVo> list = JSONObject.parseArray(JSONObject.toJSONString(result.getData()), ChannelDataExportVo.class);
                if (!ExcelUtils.downloadExcel(response,list,ChannelDataExportVo.class,"渠道统计","渠道统计")){
                    return new Result(CodeEnum.FAIL.getCode(),"渠道统计导出失败");
                }
            }

        }
        return new Result(CodeEnum.SUCCESS.getCode(),"导出成功");
    }

    @ApiOperation("中奖金额数据统计")
    @PostMapping("/getWinPrizeData")
    public Result getWinPrizeData(@ApiParam(value = "开始时间",required = false) String startTime,
                                  @ApiParam(value = "结束时间",required = false) String endTime,
                                  @ApiParam(value = "彩种",required = false) String lotteryId){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("startTime",startTime);
        paramMap.put("endTime",endTime);
        paramMap.put("lotteryId",lotteryId);
        return lotteryServerFeignClient.getWinPrizeData(paramMap);
    }

    @ApiOperation("系统配置参数列表")
    @PostMapping("/getSysConfigList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, paramType = "query", dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "Integer", defaultValue = "10"),
            @ApiImplicitParam(name = "id",value = "id",required = false,dataType = "Long",paramType = "query"),
    })
    public Result getSysConfigList(String pageNo,String pageSize,Long id){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("id",id);
        paramMap.put("pageNo",pageNo);
        paramMap.put("pageSize",pageSize);
        return lotteryServerFeignClient.getSysConfigList(paramMap);
    }

    @ApiOperation("修改系统配置参数")
    @PostMapping("/updateSysConfig")
    public Result updateSysConfig(SysConfigVO vo){
        return lotteryServerFeignClient.updateSysConfig(vo);
    }

}
